package lease

import (
	"gitverse.ru/synapse/kubelatte/pkg/logs"
	"sync"
	"time"
)

type Status int

var mu sync.Mutex

var lease Status = NotSet

const (
	NotSet Status = iota
	Leader
	Slave
)

func DoesLeaseStatusSet() bool {
	return lease != NotSet
}

func AmILeader() bool {
	return lease == Leader
}

func AmISlave() bool {
	return lease == Slave
}

func SetMeLeader() {
	mu.Lock()
	defer mu.Unlock()
	lease = Leader
}

func SetMeSlave() {
	mu.Lock()
	defer mu.Unlock()
	lease = Slave
}

func SetMeWithoutStatus() {
	mu.Lock()
	defer mu.Unlock()
	lease = NotSet
}

func WaitForLeaderNotice() {
	for lease == NotSet {
		logs.Logger.Info("Waiting for leader")
		time.Sleep(250 * time.Millisecond)
	}
}
